package com.dreem.principle.demeter;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: DesignParrtern
 * @description: 迪米特法则
 * @author: Peng
 * @create: 2021-01-18  21:26
 **/
public class Demeter {
    public static void main(String[] args){
        new SchoolManager().printAllEmployee(new CollegeManager());
    }
}

// 员工类
abstract class Employee{
    private String id;
    public abstract String getId();
    public abstract void setId(String id);
}

// 学院员工类
class CollegeEmployee extends Employee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学校员工类
class SchoolEmployee extends Employee {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

// 学院管理类
class CollegeManager{
    public List<CollegeEmployee> getAllEmployee(){
        List<CollegeEmployee> collegeEmployeeList = new ArrayList<CollegeEmployee>(10);
        for (int i = 0; i < 10; i++) {
            CollegeEmployee temp = new CollegeEmployee();
            temp.setId("学院员工  " + i);
            collegeEmployeeList.add(temp);
        }
        return collegeEmployeeList;
    }
}

// 学校管理类
class SchoolManager{
    public List<SchoolEmployee> getAllEmployee(){
        List<SchoolEmployee> schoolEmployeeList = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            SchoolEmployee temp = new SchoolEmployee();
            temp.setId("学校员工  " + i);
            schoolEmployeeList.add(temp);
        }
        return schoolEmployeeList;
    }
    public void printAllEmployee(CollegeManager collegeManager){
        // 分析问题
        // 1.这里的CollegeEmployee 不是SchoolManager的直接朋友
        // 2.CollegeEmployee 是以局部变量方式出现在SchoolManager
        // 3.违反了迪米特法则
        List<SchoolEmployee> allEmployee = this.getAllEmployee();
        System.out.println("-----------学校员工如下----------");
        for(SchoolEmployee schoolEmployee : allEmployee){
            System.out.println(schoolEmployee.getId());
        }
        List<CollegeEmployee> allEmployee1 = collegeManager.getAllEmployee();
        System.out.println("-----------学校学院员工如下----------");
        for(CollegeEmployee collegeEmployee : allEmployee1){
            System.out.println(collegeEmployee.getId());
        }
    }
}